//
//  InteractiveTransition.m
//  BriefBookAnimation
//
//  Created by Burn on 2019/6/27.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "InteractiveTransition.h"
#import "PresentOneTransition.h"
@interface InteractiveTransition ()

@property(weak,nonatomic) UIViewController *vc;
@property(assign,nonatomic) GestureDirecation direction;
@property(assign,nonatomic) InteractiveTransitionType type;

@end

@implementation InteractiveTransition


+ (instancetype)interactiveTransitionWithTransitionType:(InteractiveTransitionType)type GestureDirection:(GestureDirecation)direction{
    
    return [[InteractiveTransition alloc]initWithTransitionType:type GestureDirection:direction];
}

- (instancetype)initWithTransitionType:(InteractiveTransitionType)type GestureDirection:(GestureDirecation)direction{
    if (self = [super init]) {
        self.type = type;
        self.direction = direction;
    }
    return self;
}


- (void)addPanGestureForViewController:(UIViewController *)vc{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    self.vc = vc;
    [vc.view addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)ges{
    
    
    CGFloat progress = 0;
    CGFloat transitionX = 0;
    CGFloat transitionY = 0;
    switch (_direction) {
        case GestureDirecationLeft:
            transitionX = -[ges translationInView:ges.view].x;
            progress = transitionX / ges.view.frame.size.width;
//            NSLog(@"GestureDirecationLeft%f",progress);
            break;
        case GestureDirecationRight:
            transitionX = [ges translationInView:ges.view].x;
            progress = transitionX / ges.view.frame.size.width;
//            NSLog(@"GestureDirecationRight%f",progress);
            break;
        case GestureDirecationUp:
            transitionY = -[ges translationInView:ges.view].y / 2;
            progress = transitionY / (ges.view.frame.size.height) ;
//            NSLog(@"GestureDirecationUp%f",progress);
            break;
        case GestureDirecationDown:
            transitionY = [ges translationInView:ges.view].y;
            progress = transitionY / ges.view.frame.size.height;
//            NSLog(@"GestureDirecationDown%f",progress);
            break;
            
        default:
            break;
    }
    
    
    
    switch (ges.state) {
        case UIGestureRecognizerStateBegan:
            //  手势开始的时候标记手指状态,平开始响应的事件,他的作用在使用这个类的时候说明
            self.interation = YES;
            //  手势开始时触发对应的转场操作,方法代码在后面
            [self starGesture];
            break;
        case UIGestureRecognizerStateChanged:
            //  手势过程中,通过updateInteractiveTransition设置pop过程进行百分比
            [self updateInteractiveTransition:progress];
            break;
        case UIGestureRecognizerStateEnded:
            //  手势完成后结束标记并且判断移动距离是否过半,过则finishInteractiveTransition完成转场操作,否者取消转场操作
            self.interation = NO;
            if (transitionY > 50) {
                [self finishInteractiveTransition];
            }else{
                [self cancelInteractiveTransition];
            }
            break;
            
        default:
            break;
    }
}

- (void)starGesture{
    switch (self.type) {
        case InteractiveTransitionTypePresent:
            if (_presentConifg) {
                _presentConifg();
            }
            break;
        case InteractiveTransitionTypeDismiss:
            [self.vc dismissViewControllerAnimated:YES completion:nil];
            break;
            
        case InteractiveTransitionTypePush:
            if (_pushConifg) {
                _pushConifg();
            }
            break;
            case InteractiveTransitionTypePop:
            [self.vc.navigationController popViewControllerAnimated:YES];
            break;
        default:
            break;
    }
}
@end
